package com.cencat.product.dto;

import com.cencat.product.entity.Product;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 产品创建DTO
 * 
 * @author cencat
 * @since 2024-01-01
 */
@Data
@Schema(description = "产品创建DTO")
public class ProductCreateDTO {

    /**
     * 产品编码
     */
    @NotBlank(message = "产品编码不能为空")
    @Size(max = 50, message = "产品编码长度不能超过50个字符")
    @Pattern(regexp = "^[A-Z0-9_-]+$", message = "产品编码只能包含大写字母、数字、下划线和横线")
    @Schema(description = "产品编码", example = "PRD_001")
    private String productCode;

    /**
     * 产品名称
     */
    @NotBlank(message = "产品名称不能为空")
    @Size(max = 200, message = "产品名称长度不能超过200个字符")
    @Schema(description = "产品名称", example = "iPhone 15 Pro Max")
    private String productName;

    /**
     * 产品简称
     */
    @Size(max = 100, message = "产品简称长度不能超过100个字符")
    @Schema(description = "产品简称", example = "iPhone 15 Pro")
    private String shortName;

    /**
     * 产品描述
     */
    @Size(max = 1000, message = "产品描述长度不能超过1000个字符")
    @Schema(description = "产品描述")
    private String description;

    /**
     * 产品详情
     */
    @Schema(description = "产品详情（富文本）")
    private String detail;

    /**
     * 产品分类ID
     */
    @NotNull(message = "产品分类不能为空")
    @Min(value = 1, message = "产品分类ID必须大于0")
    @Schema(description = "产品分类ID", example = "1")
    private Long categoryId;

    /**
     * 品牌ID
     */
    @Min(value = 1, message = "品牌ID必须大于0")
    @Schema(description = "品牌ID", example = "1")
    private Long brandId;

    /**
     * 商家ID
     */
    @NotNull(message = "商家ID不能为空")
    @Min(value = 1, message = "商家ID必须大于0")
    @Schema(description = "商家ID", example = "1")
    private Long merchantId;

    /**
     * 产品类型
     */
    @NotNull(message = "产品类型不能为空")
    @Schema(description = "产品类型", example = "PHYSICAL")
    private Product.ProductType productType;

    /**
     * 产品状态
     */
    @Schema(description = "产品状态", example = "DRAFT")
    private Product.ProductStatus status = Product.ProductStatus.DRAFT;

    /**
     * 销售价格
     */
    @NotNull(message = "销售价格不能为空")
    @DecimalMin(value = "0.01", message = "销售价格必须大于0")
    @Digits(integer = 8, fraction = 2, message = "销售价格格式不正确")
    @Schema(description = "销售价格", example = "9999.99")
    private BigDecimal salePrice;

    /**
     * 市场价格
     */
    @DecimalMin(value = "0.00", message = "市场价格不能小于0")
    @Digits(integer = 8, fraction = 2, message = "市场价格格式不正确")
    @Schema(description = "市场价格", example = "10999.99")
    private BigDecimal marketPrice;

    /**
     * 成本价格
     */
    @DecimalMin(value = "0.00", message = "成本价格不能小于0")
    @Digits(integer = 8, fraction = 2, message = "成本价格格式不正确")
    @Schema(description = "成本价格", example = "8000.00")
    private BigDecimal costPrice;

    /**
     * 库存数量
     */
    @NotNull(message = "库存数量不能为空")
    @Min(value = 0, message = "库存数量不能小于0")
    @Schema(description = "库存数量", example = "100")
    private Integer stockQuantity;

    /**
     * 预警库存
     */
    @Min(value = 0, message = "预警库存不能小于0")
    @Schema(description = "预警库存", example = "10")
    private Integer warningStock = 0;

    /**
     * 最小库存
     */
    @Min(value = 0, message = "最小库存不能小于0")
    @Schema(description = "最小库存", example = "5")
    private Integer minStock = 0;

    /**
     * 最大库存
     */
    @Min(value = 0, message = "最大库存不能小于0")
    @Schema(description = "最大库存", example = "1000")
    private Integer maxStock = 0;

    /**
     * 产品重量（克）
     */
    @DecimalMin(value = "0.00", message = "产品重量不能小于0")
    @Digits(integer = 8, fraction = 2, message = "产品重量格式不正确")
    @Schema(description = "产品重量（克）", example = "200.50")
    private BigDecimal weight;

    /**
     * 产品体积（立方厘米）
     */
    @DecimalMin(value = "0.00", message = "产品体积不能小于0")
    @Digits(integer = 10, fraction = 2, message = "产品体积格式不正确")
    @Schema(description = "产品体积（立方厘米）", example = "1500.00")
    private BigDecimal volume;

    /**
     * 产品尺寸
     */
    @Size(max = 100, message = "产品尺寸长度不能超过100个字符")
    @Schema(description = "产品尺寸", example = "160.9×77.6×7.8mm")
    private String dimensions;

    /**
     * 产品颜色
     */
    @Size(max = 50, message = "产品颜色长度不能超过50个字符")
    @Schema(description = "产品颜色", example = "深空黑色")
    private String color;

    /**
     * 产品材质
     */
    @Size(max = 100, message = "产品材质长度不能超过100个字符")
    @Schema(description = "产品材质", example = "钛金属")
    private String material;

    /**
     * 产品产地
     */
    @Size(max = 100, message = "产品产地长度不能超过100个字符")
    @Schema(description = "产品产地", example = "中国")
    private String origin;

    /**
     * 保质期（天）
     */
    @Min(value = 0, message = "保质期不能小于0")
    @Schema(description = "保质期（天）", example = "365")
    private Integer shelfLife;

    /**
     * 生产日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "生产日期", example = "2024-01-01 10:00:00")
    private LocalDateTime productionDate;

    /**
     * 过期日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "过期日期", example = "2025-01-01 10:00:00")
    private LocalDateTime expiryDate;

    /**
     * 产品图片列表
     */
    @Valid
    @Schema(description = "产品图片列表")
    private List<ProductImageDTO> images;

    /**
     * 产品视频列表
     */
    @Valid
    @Schema(description = "产品视频列表")
    private List<ProductVideoDTO> videos;

    /**
     * 产品标签
     */
    @Size(max = 500, message = "产品标签长度不能超过500个字符")
    @Schema(description = "产品标签（逗号分隔）", example = "热销,推荐,新品")
    private String tags;

    /**
     * 产品关键词
     */
    @Size(max = 500, message = "产品关键词长度不能超过500个字符")
    @Schema(description = "产品关键词（逗号分隔）", example = "手机,智能手机,苹果")
    private String keywords;

    /**
     * SEO标题
     */
    @Size(max = 200, message = "SEO标题长度不能超过200个字符")
    @Schema(description = "SEO标题")
    private String seoTitle;

    /**
     * SEO描述
     */
    @Size(max = 500, message = "SEO描述长度不能超过500个字符")
    @Schema(description = "SEO描述")
    private String seoDescription;

    /**
     * SEO关键词
     */
    @Size(max = 500, message = "SEO关键词长度不能超过500个字符")
    @Schema(description = "SEO关键词")
    private String seoKeywords;

    /**
     * 是否推荐
     */
    @Schema(description = "是否推荐", example = "false")
    private Boolean isRecommended = false;

    /**
     * 是否热销
     */
    @Schema(description = "是否热销", example = "false")
    private Boolean isHot = false;

    /**
     * 是否新品
     */
    @Schema(description = "是否新品", example = "true")
    private Boolean isNew = false;

    /**
     * 是否特价
     */
    @Schema(description = "是否特价", example = "false")
    private Boolean isSpecial = false;

    /**
     * 排序号
     */
    @Min(value = 0, message = "排序号不能小于0")
    @Schema(description = "排序号", example = "0")
    private Integer sortOrder = 0;

    /**
     * 上架时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "上架时间", example = "2024-01-01 10:00:00")
    private LocalDateTime shelfTime;

    /**
     * 产品规格列表
     */
    @Valid
    @Schema(description = "产品规格列表")
    private List<ProductSpecDTO> specifications;

    /**
     * 产品属性列表
     */
    @Valid
    @Schema(description = "产品属性列表")
    private List<ProductAttributeDTO> attributes;

    /**
     * 备注
     */
    @Size(max = 500, message = "备注长度不能超过500个字符")
    @Schema(description = "备注")
    private String remark;

    /**
     * 产品图片DTO
     */
    @Data
    @Schema(description = "产品图片DTO")
    public static class ProductImageDTO {
        
        /**
         * 图片URL
         */
        @NotBlank(message = "图片URL不能为空")
        @Size(max = 500, message = "图片URL长度不能超过500个字符")
        @Schema(description = "图片URL")
        private String url;
        
        /**
         * 图片标题
         */
        @Size(max = 100, message = "图片标题长度不能超过100个字符")
        @Schema(description = "图片标题")
        private String title;
        
        /**
         * 图片描述
         */
        @Size(max = 200, message = "图片描述长度不能超过200个字符")
        @Schema(description = "图片描述")
        private String description;
        
        /**
         * 是否主图
         */
        @Schema(description = "是否主图")
        private Boolean isMain = false;
        
        /**
         * 排序号
         */
        @Min(value = 0, message = "排序号不能小于0")
        @Schema(description = "排序号")
        private Integer sortOrder = 0;
    }

    /**
     * 产品视频DTO
     */
    @Data
    @Schema(description = "产品视频DTO")
    public static class ProductVideoDTO {
        
        /**
         * 视频URL
         */
        @NotBlank(message = "视频URL不能为空")
        @Size(max = 500, message = "视频URL长度不能超过500个字符")
        @Schema(description = "视频URL")
        private String url;
        
        /**
         * 视频标题
         */
        @Size(max = 100, message = "视频标题长度不能超过100个字符")
        @Schema(description = "视频标题")
        private String title;
        
        /**
         * 视频描述
         */
        @Size(max = 200, message = "视频描述长度不能超过200个字符")
        @Schema(description = "视频描述")
        private String description;
        
        /**
         * 视频时长（秒）
         */
        @Min(value = 0, message = "视频时长不能小于0")
        @Schema(description = "视频时长（秒）")
        private Integer duration;
        
        /**
         * 视频封面
         */
        @Size(max = 500, message = "视频封面URL长度不能超过500个字符")
        @Schema(description = "视频封面URL")
        private String coverUrl;
        
        /**
         * 排序号
         */
        @Min(value = 0, message = "排序号不能小于0")
        @Schema(description = "排序号")
        private Integer sortOrder = 0;
    }

    /**
     * 产品规格DTO
     */
    @Data
    @Schema(description = "产品规格DTO")
    public static class ProductSpecDTO {
        
        /**
         * 规格名称
         */
        @NotBlank(message = "规格名称不能为空")
        @Size(max = 50, message = "规格名称长度不能超过50个字符")
        @Schema(description = "规格名称")
        private String name;
        
        /**
         * 规格值
         */
        @NotBlank(message = "规格值不能为空")
        @Size(max = 200, message = "规格值长度不能超过200个字符")
        @Schema(description = "规格值")
        private String value;
        
        /**
         * 规格单位
         */
        @Size(max = 20, message = "规格单位长度不能超过20个字符")
        @Schema(description = "规格单位")
        private String unit;
        
        /**
         * 排序号
         */
        @Min(value = 0, message = "排序号不能小于0")
        @Schema(description = "排序号")
        private Integer sortOrder = 0;
    }

    /**
     * 产品属性DTO
     */
    @Data
    @Schema(description = "产品属性DTO")
    public static class ProductAttributeDTO {
        
        /**
         * 属性名称
         */
        @NotBlank(message = "属性名称不能为空")
        @Size(max = 50, message = "属性名称长度不能超过50个字符")
        @Schema(description = "属性名称")
        private String name;
        
        /**
         * 属性值
         */
        @NotBlank(message = "属性值不能为空")
        @Size(max = 200, message = "属性值长度不能超过200个字符")
        @Schema(description = "属性值")
        private String value;
        
        /**
         * 属性类型
         */
        @Size(max = 20, message = "属性类型长度不能超过20个字符")
        @Schema(description = "属性类型")
        private String type;
        
        /**
         * 是否必填
         */
        @Schema(description = "是否必填")
        private Boolean required = false;
        
        /**
         * 排序号
         */
        @Min(value = 0, message = "排序号不能小于0")
        @Schema(description = "排序号")
        private Integer sortOrder = 0;
    }
}